package goroutine

import (
	"gitee.com/baizhige/go-server-tempalte/common/log"
	"runtime/debug"
	"sync"
)

// goroutine.go 协程工具类 对外能力

func Safe(f func()) {
	go func() {
		defer func() {
			if r := recover(); r != nil {
				log.ErrorF("Panic recovered from panic: %v ,stack:%s", r, string(debug.Stack()))
			}
		}()
		f()
	}()
}

func SafeWg(wg *sync.WaitGroup, f func()) {
	go func() {
		defer func() {
			if r := recover(); r != nil {
				log.ErrorF("Panic recovered from panic: %v ,stack:%s", r, string(debug.Stack()))
			}
			wg.Done()
		}()
		f()
	}()
}
